# 网站中间件层, 所以关于网站的请求都必须经过此中间件
from django.utils.deprecation import MiddlewareMixin


from django.http import JsonResponse
from django.shortcuts import redirect

import time

# 网站中间件
class Web(MiddlewareMixin):
    def process_request(self, request):
        # 获取域名 拼接
        request.IncentiveHubURL = request.scheme + "://" + request.META.get("HTTP_HOST")

        # 判断当前路径是否是网站后台
        if request.path.startswith("/web/ht/"):
            # 判断当前页面是否是登录页面,如果是,则不重定向
            if request.path in ["/web/ht/login/"]:
                pass
            else:
                # 验证用户是否登录,在本地是否有session
                if request.session.get("user"):
                    pass
                else:
                    return redirect("/web/ht/login/")
        return None
    def process_response(self, request, response):
        return response



